package com.chenjj.java.test.juc.tools;

import com.chenjj.java.juc.tools.semaphore.People;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaphoreTester {

    @Test
    public void test1() throws InterruptedException {
        //定义食堂，最高只能允许2个人同时吃饭
        Semaphore semaphore = new Semaphore(2);

        //定义20个食客
        List<People> peoples = new ArrayList<>();
        for (int i = 0 ; i < 20 ; i++){
            People p = new People(semaphore);
            peoples.add(p);
        }

        //吃饭
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (People p :
                peoples) {
            executorService.execute(p);
        }


        Thread.sleep(60*1000);
    }
}
